package models

import (
	"bigdevops/src/common"
	"bigdevops/src/config"
	"encoding/json"
	"fmt"
	"strings"
)

var (
	canaryYamlString1 = `
steps:
    - replicas: 1
    - replicas: 50%
    - replicas: 100%
`
)

func MockCicd(sc *config.ServerConfig) {

	// 先造几个服务发布项
	// 查询出所有的叶子节点

	LeafTreeNodes, _ := GetStreeNodeAllLeaf()
	for _, LeafTreeNode := range LeafTreeNodes {
		LeafTreeNode := LeafTreeNode
		LeafTreeNode.FillFrontAllData()
	}

	num := len(LeafTreeNodes)
	//clusters, _ := GetK8sClusterAll()
	k8sApps, _ := GetK8sAppAll()
	//clusterNames := []string{}
	stages := []*DeployStage{}
	for _, k8sApp := range k8sApps {
		k8sApp := k8sApp
		if !strings.Contains(k8sApp.Name, "go01") {
			continue
		}
		k8sApp.FillFrontAllData()
		stages = append(stages, &DeployStage{
			Name:     fmt.Sprintf("预发-%v-%v", k8sApp.Cluster, k8sApp.Name),
			Cluster:  k8sApp.Cluster,
			K8sAppId: k8sApp.ID,
			Replicas: 5,
		})

	}

	stagesBytes, _ := json.Marshal(stages)

	for i := 0; i < num; i++ {
		obj := CicdDeployItem{
			RunType:    "k8s",
			UserID:     1,
			TreeNodeId: LeafTreeNodes[i].ID,
			// {'label': 'devops/go-app-01', 'value': 2}
			GitlabProjectId:          2,
			IsCanary:                 true,
			CanaryStrategyYamlString: canaryYamlString1,
			GitlabRepository:         "devops/go-app-01",
			ImagePrefix:              "192.168.0.81:30002/devops/go-app-01",
			Stages:                   string(stagesBytes),
		}
		obj.CreateOne()
	}

	// 查询所有的user

	for i := 0; i < num; i++ {
		// 获取服务发布项对象
		itemObj, err := GetCicdDeployItemById(i)
		if err != nil {
			continue
		}

		//nodeObj := LeafTreeNodes[i]

		obj := CicdWorkOrder{
			Name:              fmt.Sprintf("发布工单-%v", i),
			UserID:            1,
			GitTag:            "v1.0.0",
			Status:            common.WorkOrderInstanceStatusPendingApproval,
			CicdDeployItemId:  itemObj.ID,
			ApprovalStatus:    common.WorkOrderInstanceStatusPendingApproval,
			CicdDeployItemObj: itemObj,
		}

		obj.TransactionCreate()
	}

}
